package com.qiaofang.applet.rest;

import com.qiaofang.applet.common.dto.DataResponse;
import com.qiaofang.applet.service.other.service.dto.*;
import com.qiaofang.applet.service.other.util.AverageCapitalPlusInterestUtils;
import com.qiaofang.applet.service.other.util.AverageCapitalUtils;
import com.qiaofang.applet.service.other.util.LoanConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;

/**
 * 贷款计算器
 *
 * @author daiyuxiang
 * @version 2018-10-12
 */
@RestController
@RequestMapping(value = "/api/pcweb/loan")
@Api(tags = "贷款：贷款计算器")
public class LoanController {

    @PostMapping("/accumulationAndBusinessLoan")
    @ApiOperation(value = "公积金和商业贷款", notes = "公积金和商业贷款")
    public DataResponse<LoanResultDto> accumulationAndBusinessLoan(@RequestBody @Valid LoanDto loanDTO) {
        LoanResultDto loanResultDTO = new LoanResultDto();
        if ("1".equals(loanDTO.getRepayType())) {
            loanResultDTO = AverageCapitalPlusInterestUtils.getLoanResultDTO(loanDTO);
            loanResultDTO.setPerMonthPrincipalInterest(new BigDecimal(loanResultDTO.getPerMonthPrincipalInterest()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        } else {
            loanResultDTO = AverageCapitalUtils.getLoanResultDTO(loanDTO);
            loanResultDTO.setPerMonthFirst(new BigDecimal(loanResultDTO.getPerMonthFirst()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            loanResultDTO.setPerMonthLast(new BigDecimal(loanResultDTO.getPerMonthLast()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        }
        loanResultDTO.setInterestCount(new BigDecimal(loanResultDTO.getInterestCount() / 10000).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

        return DataResponse.ok(loanResultDTO);
    }

    @PostMapping("/combinationLoan")
    @ApiOperation(value = "组合贷款", notes = "组合贷款")
    public DataResponse<LoanResultDto> combinationLoan(@RequestBody @Valid CombinationLoanDto combinationLoanDTO) {
        //商业
        LoanDto businessDTO = new LoanDto();
        businessDTO.setTotalYear(combinationLoanDTO.getTotalYear());
        businessDTO.setInvest(combinationLoanDTO.getInvestBusiness());
        businessDTO.setYearRate(combinationLoanDTO.getYearRateBusiness());
        //公积金
        LoanDto accumulationDTO = new LoanDto();
        accumulationDTO.setTotalYear(combinationLoanDTO.getTotalYear());
        accumulationDTO.setInvest(combinationLoanDTO.getInvestAccumulation());
        accumulationDTO.setYearRate(combinationLoanDTO.getYearRateAccumulation());

        LoanResultDto businessResultDTO = new LoanResultDto();
        LoanResultDto accumulationResultDTO = new LoanResultDto();
        LoanResultDto loanResultDTO = new LoanResultDto();
        if ("1".equals(combinationLoanDTO.getRepayType())) {
            businessResultDTO = AverageCapitalPlusInterestUtils.getLoanResultDTO(businessDTO);
            accumulationResultDTO = AverageCapitalPlusInterestUtils.getLoanResultDTO(accumulationDTO);
            loanResultDTO.setPerMonthPrincipalInterest(new BigDecimal(businessResultDTO.getPerMonthPrincipalInterest() + accumulationResultDTO.getPerMonthPrincipalInterest()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

        } else {
            businessResultDTO = AverageCapitalUtils.getLoanResultDTO(businessDTO);
            accumulationResultDTO = AverageCapitalUtils.getLoanResultDTO(accumulationDTO);
            loanResultDTO.setPerMonthFirst(new BigDecimal(businessResultDTO.getPerMonthFirst() + accumulationResultDTO.getPerMonthFirst()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            loanResultDTO.setPerMonthLast(new BigDecimal(businessResultDTO.getPerMonthLast() + accumulationResultDTO.getPerMonthLast()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

        }
        loanResultDTO.setInterestCount(new BigDecimal((businessResultDTO.getInterestCount() + accumulationResultDTO.getInterestCount()) / 10000).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

        return DataResponse.ok(loanResultDTO);
    }

    @PostMapping("/taxCalculation")
    @ApiOperation(value = "税费计算", notes = "税费计算")
    public DataResponse<TaxResultDto> taxCalculation(@RequestBody @Valid TaxDto taxDTO) {
        BigDecimal totalPrice = new BigDecimal(taxDTO.getTotalPrice());
        TaxResultDto taxResultDTO = new TaxResultDto();

        //买方税费计算
        if (LoanConstant.TRANSACTION_TYPE_1.equals(taxDTO.getTransactionType())) {
            String familyNum = taxDTO.getFamilyNum();
            BigDecimal area = new BigDecimal(taxDTO.getArea());

            BigDecimal area90 = new BigDecimal("90");

            if (LoanConstant.HOUSE_PROPERTY_1.equals(taxDTO.getHouseProperty())) {
                //契税计算
                if (LoanConstant.FAMILY_NUM_1.equals(familyNum)) {
                    // 大于90
                    if (area.compareTo(area90) == 1) {
                        taxResultDTO.setDeedTax(totalPrice.multiply(new BigDecimal("0.015")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    } else {
                        taxResultDTO.setDeedTax(totalPrice.multiply(new BigDecimal("0.01")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    }
                } else if (LoanConstant.FAMILY_NUM_2.equals(familyNum)) {
                    // 大于90
                    if (area.compareTo(area90) == 1) {
                        taxResultDTO.setDeedTax(totalPrice.multiply(new BigDecimal("0.02")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    } else {
                        taxResultDTO.setDeedTax(totalPrice.multiply(new BigDecimal("0.01")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    }
                } else if (LoanConstant.FAMILY_NUM_3.equals(familyNum)) {
                    taxResultDTO.setDeedTax(totalPrice.multiply(new BigDecimal("0.03")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                }
            } else {
                taxResultDTO.setDeedTax(totalPrice.multiply(new BigDecimal("0.03")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            }
        } else if (LoanConstant.TRANSACTION_TYPE_2.equals(taxDTO.getTransactionType())) {
            //卖方税费计算
            if (LoanConstant.HOUSE_PROPERTY_1.equals(taxDTO.getHouseProperty())) {
                if (LoanConstant.BUY_YEAR_3.equals(taxDTO.getBuyYear())) {
                    //增值税
                    taxResultDTO.setAddedTax(totalPrice.multiply(new BigDecimal("0.05")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    //城市维护费
                    taxResultDTO.setMaintainTax(totalPrice.multiply(new BigDecimal("0.00175")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    //教育费附加
                    taxResultDTO.setEducationTax(totalPrice.multiply(new BigDecimal("0.00125")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                } else {
                    taxResultDTO.setAddedTax(new BigDecimal("0").setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    //城市维护费
                    taxResultDTO.setMaintainTax(new BigDecimal("0").setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    //教育费附加
                    taxResultDTO.setEducationTax(new BigDecimal("0").setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                }
                if (LoanConstant.BUY_YEAR_3.equals(taxDTO.getBuyYear()) || LoanConstant.BUY_YEAR_2.equals(taxDTO.getBuyYear()) || LoanConstant.OFF_HOT.equals(taxDTO.getIsOnly())) {
                    taxResultDTO.setIncomeTax(totalPrice.multiply(new BigDecimal("0.01")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                } else {
                    taxResultDTO.setIncomeTax(new BigDecimal("0").setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                }
            } else if (LoanConstant.HOUSE_PROPERTY_2.equals(taxDTO.getHouseProperty())) {
                //个人所得税
                taxResultDTO.setIncomeTax(totalPrice.multiply(new BigDecimal("0.02")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                //土地增值税
                taxResultDTO.setLandAddedTax(totalPrice.multiply(new BigDecimal("0.03")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                //印花税
                taxResultDTO.setPrintingTax(totalPrice.multiply(new BigDecimal("0.00025")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                //增值税
                taxResultDTO.setAddedTax(totalPrice.multiply(new BigDecimal("0.05")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                //城市维护费
                taxResultDTO.setMaintainTax(totalPrice.multiply(new BigDecimal("0.00175")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                //教育费附加
                taxResultDTO.setEducationTax(totalPrice.multiply(new BigDecimal("0.00125")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            }
            String assignType = taxDTO.getAssignType();
            if (LoanConstant.ASSIGN_TYPE_2.equals(assignType)) {
                taxResultDTO.setAssignTax(totalPrice.multiply(new BigDecimal("0.02")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            } else if (LoanConstant.ASSIGN_TYPE_3.equals(assignType)) {
                taxResultDTO.setAssignTax(totalPrice.multiply(new BigDecimal("0.01")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            }
        }

        return DataResponse.ok(taxResultDTO);
    }
}
